package com.gky.gkytemplate.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gky.gkytemplate.common.enums.ErrorCode;
import com.gky.gkytemplate.common.exception.BusinessException;
import com.gky.gkytemplate.common.result.Result;
import com.gky.gkytemplate.pojo.dto.team.TeamAddRequest;
import com.gky.gkytemplate.pojo.dto.team.TeamDeleteRequest;
import com.gky.gkytemplate.pojo.dto.team.TeamQueryRequest;
import com.gky.gkytemplate.pojo.dto.team.TeamUpdateRequest;
import com.gky.gkytemplate.pojo.entity.Team;
import com.gky.gkytemplate.pojo.vo.team.TeamQueryVo;
import com.gky.gkytemplate.service.TeamService;
import com.gky.gkytemplate.utils.ThrowUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/team")
@Slf4j
public class TeamController {

    @Resource
    private TeamService teamService;

    /**
     * 添加球队
     * @param teamAddRequest
     * @return
     */
    @PostMapping("/addTeam")
    public Result<Boolean> addTeam(TeamAddRequest teamAddRequest){
        ThrowUtils.throwIf(ObjectUtil.isEmpty(teamAddRequest) , ErrorCode.NOT_FOUND_ERROR);
        log.info("測試");
        log.info("測試");
        log.info("測試");
        log.info("測試");

        Team team = new Team();
        BeanUtils.copyProperties(teamAddRequest, team);
        boolean result = teamService.save(team);
        return Result.ok(result);
    }

    /**
     * 更新球队
     * @param teamUpdateRequest
     * @return
     */
    @PostMapping("/updateTeam")
    public Result<Boolean> updateTeam(@RequestBody TeamUpdateRequest teamUpdateRequest){
        ThrowUtils.throwIf(ObjectUtil.isEmpty(teamUpdateRequest) , ErrorCode.NOT_FOUND_ERROR);
        Team team = new Team();
        log.info("id{}",teamUpdateRequest.getId());
        BeanUtils.copyProperties(teamUpdateRequest, team);
        boolean result = teamService.updateById(team);
        return Result.ok(result);
    }

    /**
     * 删除球队
     * @param teamDeleteRequest
     * @return
     */
    @DeleteMapping("/deleteTeam")
    public Result<Boolean> deleteTeam(@RequestBody TeamDeleteRequest teamDeleteRequest){
        ThrowUtils.throwIf(ObjectUtil.isEmpty(teamDeleteRequest) , ErrorCode.NOT_FOUND_ERROR);
        boolean result = teamService.removeById(teamDeleteRequest.getId());
        return Result.ok(result);
    }

    /**
     * 根据id回显数据球队数据
     * @param id
     * @return
     */
    @PostMapping("/{id}")
    public Result<TeamQueryVo> queryTeam(@PathVariable("id") Integer id){
        ThrowUtils.throwIf(id == null,ErrorCode.NOT_FOUND_ERROR);

        Team team = teamService.getById(id);
        if (team == null){
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        TeamQueryVo teamQueryVo = new TeamQueryVo();
        BeanUtils.copyProperties(team,teamQueryVo);

        return Result.ok(teamQueryVo);
    }

    /**
     * 分页查询球队
     * @param teamQueryRequest
     * @return
     */
    @PostMapping ("/page")
    public Result<Page<Team>> pageQuery(@RequestBody TeamQueryRequest teamQueryRequest){

        ThrowUtils.throwIf(teamQueryRequest == null,ErrorCode.NOT_FOUND_ERROR);

        Page<Team> teamPage = teamService.listPage(teamQueryRequest);
        return Result.ok(teamPage);
    }

}
